package SpotList;

public class SpotList implements ISpotList {
    int len;
    Spot[] element;

    public SpotList(int size){
        this.element = new Spot[size];
        this.len = 0;
    }

    @Override
    public boolean isEmpty() {
        return this.len == 0;
    }

    @Override
    public int length() {
        return this.len;
    }

    @Override
    public int getId(int i) {
        return this.element[i].id;
    }

    @Override
    public String getName(int i) {
        return this.element[i].getName();
    }

    @Override
    public String getIntro(int i) {
        return this.element[i].getIntro();
    }

    @Override
    public String getAdj(int i) {
        return this.element[i].getAdj();
    }

    @Override
    public Spot get(int i) {
        return this.element[i];
    }

    @Override
    public void setId(int i, int id) {
        this.element[i].setId(id);
    }

    @Override
    public void setName(int i, String name) {
        this.element[i].setName(name);
    }

    @Override
    public void setIntro(int i, String intro) {
        this.element[i].setIntro(intro);
    }

    @Override
    public void setAdj(int i, String adj) {
        this.element[i].setAdj(adj);
    }

    @Override
    public void remove(int i) {
        if(this.len == 0 || i < 0 || i >= this.len){//检查插入的景点是否合法
            System.out.println("delete position=" + i + ",is out index.");
            return;
        }
        Spot deleted = this.element[i];
        System.out.println("node:" + deleted + ",is deleted from seqlist.");
        for(int j = i; j < this.len - 1; j++){//把i后的数据元素依次前移
            this.element[j] = this.element[j + 1];
        }
        this.element[this.len - 1] = null;
        this.len--;
        return;
    }

    @Override
    public void removeAll() {
        this.len = 0;
    }

    @Override
    public int search(Object key) {
        return 0;
    }

    @Override
    public Spot[] getElements() {
        return new Spot[0];
    }

    @Override
    public void lengthen(Spot x) {
        insert(this.len, x);
    }

    @Override
    public void insert(int i, Spot x) {
        if (x == null){
            return;
        }
        if(i < 0){//阻止插入到错误下标的值
            i = 0;
        }
        if (this.len == element.length){//满串扩容
            Spot[] temp = this.element;
            this.element = new Spot[temp.length + 1];
            for(int j = 0;j < temp.length; j++){
                this.element[j] = temp[j];
            }
        }
        for(int j = this.len - 1;j >= i;j--){
            this.element[j + 1] = this.element[j];
        }
        element[i] = x;
        this.len++;
    }

    @Override
    public void set(int id, Spot x) {
        element[id].Spot(x.id, x.name, x.intro, x.adj);
    }
}
